import 'package:flutter/material.dart';
import 'package:flutter_getx/GetXControllerExample/MyController.dart';
import 'package:get/get.dart';

class GetXControllerExample extends StatelessWidget {
  GetXControllerExample({super.key});

  final MyController myController = Get.put(MyController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("GetXController"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Obx(
              () => Text(
                "我的名字是：${myController.teacher.name.value}",
              ),
            ),
            GetX<MyController>(
              init: MyController(),
              builder: (controller) {
                return Text("我的名字是：${controller.teacher.name.value}");
              },
            ),
            GetBuilder<MyController>(
              init: MyController(),
              builder: (controller) {
                return Text("我的名字是：${controller.teacher.name.value}");
              },
            ),
            ElevatedButton(
              onPressed: () {
                myController.convertToUpperCase();
              },
              child: const Text(
                "转换为大写",
              ),
            ),
            ElevatedButton(
              onPressed: () {
                Get.find<MyController>().convertToUpperCase();
              },
              child: const Text(
                "转换为大写",
              ),
            ),
          ],
        ),
      ),
    );
  }
}
